package com.azt.provider.impl;

import java.util.List;
import java.util.Map;
import java.util.Random;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.azt.api.pojo.PrizeSet;
import com.azt.api.pojo.User;
import com.azt.api.service.PrizeSetService;
import com.azt.model.page.Pagination;
import com.azt.provider.mapper.PrizeSetMapper;
import com.azt.utils.MathUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Service
@Transactional
public class PrizeSetServiceImpl implements PrizeSetService {

	@Autowired
	private PrizeSetMapper prizeSetMapper;

	@Override
	public PrizeSet luckyDraw(User user) {
		// 查询是否有内定的
		if (StringUtils.isNotBlank(user.getMobile())) {
			PrizeSet designPrize = getDesignPrize(user.getMobile(), user.getId());
			if (designPrize != null) {
				return designPrize;
			}
		}

		// 获取可用的奖品
		List<PrizeSet> usablePrize = getUsablePrize();

		if (usablePrize.size() == 0) {
			return null;
		}

		long probabilities = getPsMin(usablePrize);// 最小公倍数

		PrizeSet getPrize = null;// 抽中的奖品
		long userPro = 0;// 用户产生的随机数
		long interval = 0;
		Random random = new Random();
		if (probabilities <= Integer.MAX_VALUE) {
			userPro = random.nextInt((int) probabilities) + 1;
		} else {
			double random2 = Math.round(Math.random() * (probabilities - 1) + 1);
			userPro = new Double(random2).longValue();
		}

		for (PrizeSet prize : usablePrize) {
			long multiple = probabilities / prize.getProbability();
			if (userPro > interval && userPro <= (interval + multiple)) {
				getPrize = prize;
				break;
			}
			interval += multiple;
		}

		return getPrize;

	}

	private long getPsMin(List<PrizeSet> usablePrize) {
		long res = 0;
		if (usablePrize.size() > 0) {
			res = usablePrize.get(0).getProbability();
			for (PrizeSet p : usablePrize) {
				if (p.getProbability() > 0) {
					res = MathUtil.getMinCommonMultiple(res, p.getProbability());
				}
			}
		}
		return res;
	}

	@Override
	public List<PrizeSet> getUsablePrize() {
		return prizeSetMapper.getUsablePrize();
	}

	@Override
	public Pagination selectSetList(int pageno, int pagesize, Map<String, String> params) {
		Page<Object> startPage = PageHelper.startPage(pageno, pagesize);
		List<PrizeSet> setList = prizeSetMapper.selectSetList(params);
		Pagination p = new Pagination(startPage, setList);
		p.setList(setList);
		p.setParams(params);
		return p;
	}

	@Override
	public void saveOrUpdate(PrizeSet ps) {
		// 更新
		if (ps.getId()!= null) {
			prizeSetMapper.updateByPrimaryKeySelective(ps);
		} else {
			prizeSetMapper.insertSelective(ps);
		}
	}

	@Override
	public PrizeSet getPrizeSetById(Integer id) {
		PrizeSet ps = prizeSetMapper.selectByPrimaryKey(id);
		return ps;
	}

	// 假删
	@Override
	public void deleteById(PrizeSet ps) {
		ps.setRemoved(1);
		prizeSetMapper.updateByPrimaryKeySelective(ps);
	}

	@Override
	public PrizeSet getDesignPrize(String mobile, Integer userId) {
		return prizeSetMapper.getDesignPrize(mobile, userId);
	}
	
	@Override
	public List<PrizeSet> selectPrizeSets(Integer offset,Integer limit) {
		return prizeSetMapper.selectPrizeSets(offset,limit);
	}

	@Override
	public List<PrizeSet> selectSetList() {
		List<PrizeSet> selectAll=prizeSetMapper.selectSetList();
		return selectAll;
	}

	@Override
	public int updateDecrement(Integer id) {
		return prizeSetMapper.updateDecrement(id);
	}
}
